using System.Collections.Generic;
using UnityEngine;

public partial class ClientSessionEventBridge
{
	void RegisteMapNetEventHandlers()
	{
		ServerMessageManager.RegistMessage(80001,typeof(Map.ReqLoginMessage));
		RegistNetEventHandler(80001, OnRecvReqLoginMessage);
		ServerMessageManager.RegistMessage(80002,typeof(Map.ResLoginMessage));
		ServerMessageManager.RegistMessage(80003,typeof(Map.ReqCreateRoomMessage));
		RegistNetEventHandler(80003, OnRecvReqCreateRoomMessage);
		ServerMessageManager.RegistMessage(80004,typeof(Map.ResCreateRoomMessage));
		ServerMessageManager.RegistMessage(80005,typeof(Map.ReqCreateRoleMessage));
		RegistNetEventHandler(80005, OnRecvReqCreateRoleMessage);
		ServerMessageManager.RegistMessage(80006,typeof(Map.ResCreateRoleMessage));
		ServerMessageManager.RegistMessage(80010,typeof(Map.ReqJoinRoomMapMessage));
		RegistNetEventHandler(80010, OnRecvReqJoinRoomMapMessage);
		ServerMessageManager.RegistMessage(80011,typeof(Map.ResJoinRoomMapMessage));
		ServerMessageManager.RegistMessage(80013,typeof(Map.ReqLeaveRoomMapMessage));
		RegistNetEventHandler(80013, OnRecvReqLeaveRoomMapMessage);
		ServerMessageManager.RegistMessage(80014,typeof(Map.ResLeaveRoomMapMessage));
		ServerMessageManager.RegistMessage(80015,typeof(Map.ResRoomInfoMessage));
		ServerMessageManager.RegistMessage(80016,typeof(Map.ReqRoomBattleBeReadyMessage));
		RegistNetEventHandler(80016, OnRecvReqRoomBattleBeReadyMessage);
		ServerMessageManager.RegistMessage(80017,typeof(Map.ResBattleStartMessage));
		ServerMessageManager.RegistMessage(80018,typeof(Map.ReqLockStepCommandMessage));
		RegistNetEventHandler(80018, OnRecvReqLockStepCommandMessage);
		ServerMessageManager.RegistMessage(80019,typeof(Map.ResLockStepCommandMessage));
		ServerMessageManager.RegistMessage(80020,typeof(Map.ReqBattleEndMessage));
		RegistNetEventHandler(80020, OnRecvReqBattleEndMessage);
		ServerMessageManager.RegistMessage(80021,typeof(Map.ResBattleEndMessage));
		ServerMessageManager.RegistMessage(80050,typeof(Map.ResRoomListInfoMessage));
		ServerMessageManager.RegistMessage(80051,typeof(Map.ResRoomAddMessage));
		ServerMessageManager.RegistMessage(80052,typeof(Map.ResRoomRemovedMessage));
		ServerMessageManager.RegistMessage(80053,typeof(Map.ResRoomUpdatedMessage));
	}
	void OnRecvReqLoginMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqLoginMessage recieved !");
		if(message is Map.ReqLoginMessage recvedMsg)
		{
			var playerDoc = MongoDBManager.Instance.RegisterPlayer(recvedMsg.PlayerName, "123456");
			if(playerDoc == null)
			{
                UnityEngine.Debug.Log($"[Server]:player login failed");
                ServerNet.SendResLoginMessage(this.Id,-1,0L,0,string.Empty);
                return;
			}
			long playerId = playerDoc["Id"].AsInt64;
			int roleId = playerDoc["roleId"].AsInt32;
			string userName = playerDoc["userName"].AsString;
            ServerNetManager.Instance.RegistePlayer(this.Id, recvedMsg.PlayerName,playerId, roleId);
			UnityEngine.Debug.Log($"[Server]:player [{recvedMsg.PlayerName}] [{playerId}] logined!");
			ServerNet.SendResLoginMessage(this.Id,0, playerId,roleId,userName);
			ServerNetManager.Instance.SyncRoomListInfo(this.Id);
        }
	}
	void OnRecvReqCreateRoomMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqCreateRoomMessage recieved !");
		if(message is Map.ReqCreateRoomMessage recvedMsg)
		{
			var playerInfo = ServerNetManager.Instance.GetPlayer(this.Id);
			if (null == playerInfo)
			{
				Debug.LogError("[Server]:Create Room Failed Err:10001");
				ServerNet.SendResCreateRoomMessage(this.Id, -10001,string.Empty,string.Empty);
				return;
			}
			int roomId = ServerNetManager.Instance.CreateRoom(playerInfo.playerId, recvedMsg.RoomName,recvedMsg.RoomPassword);
			if(roomId < 0)
            {
				Debug.LogError($"[Server]:Create Room Failed Err:{-roomId}");
				ServerNet.SendResCreateRoomMessage(this.Id, roomId, string.Empty, string.Empty);
				return;
            }

			Debug.Log($"[Server]:Create Room Succeed RoomId = [{roomId}]");
			ServerNet.SendResCreateRoomMessage(this.Id, roomId,recvedMsg.RoomName, recvedMsg.RoomPassword);

			Debug.Log($"[Server]:Sync Room Info RoomId = [{roomId}]");
			ServerNetManager.Instance.SyncAddRoomInfo(roomId);
		}
	}
	void OnRecvReqCreateRoleMessage(Google.Protobuf.IMessage message)
    {
        UnityEngine.Debug.Log("[Server]:Msg ReqCreateRoleMessage recieved !");
        if (message is Map.ReqCreateRoleMessage recvedMsg)
        {
            var playerInfo = ServerNetManager.Instance.GetPlayer(this.Id);
            if (null == playerInfo)
            {
                UnityEngine.Debug.Log($"[Server]:Create Role Failed,Has No Player Yet");
				ServerNet.SendResCreateRoleMessage(this.Id, -1);
                return;
            }

			if(playerInfo.roleId != 0)
			{
                UnityEngine.Debug.Log($"[Server]:Create Role Failed,Role Already Exited Id = {playerInfo.roleId}");
                ServerNet.SendResCreateRoleMessage(this.Id, -2);
                return;
			}

			if(recvedMsg.RoleId != 1 && recvedMsg.RoleId != 2)
			{
                UnityEngine.Debug.Log($"[Server]:Create Role Failed,UnValid Role Id = {recvedMsg.RoleId}");
                ServerNet.SendResCreateRoleMessage(this.Id, -3);
                return;
            }

			MongoDBManager.Instance.UpdateRoleId(playerInfo.userName, recvedMsg.RoleId);
			ServerNet.SendResCreateRoleMessage(this.Id, recvedMsg.RoleId);
        }
    }
	void OnRecvReqJoinRoomMapMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqJoinRoomMapMessage recieved !");
		if(message is Map.ReqJoinRoomMapMessage recvedMsg)
		{

		}
	}
	void OnRecvReqLeaveRoomMapMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqLeaveRoomMapMessage recieved !");
		if(message is Map.ReqLeaveRoomMapMessage recvedMsg)
		{
            var playerInfo = ServerNetManager.Instance.GetPlayer(this.Id);
            if (null == playerInfo)
            {
                UnityEngine.Debug.Log($"[Server]:Leave Room Failed,Has No Player Yet");
				ServerNet.SendResLeaveRoomMapMessage(this.Id, -10001);
                return;
            }

			int resId = ServerNetManager.Instance.LeaveRoom(playerInfo.playerId);
			if(resId != 0)
            {
                UnityEngine.Debug.Log($"[Server]:Leave Room Failed Errcode:{resId}");
                ServerNet.SendResLeaveRoomMapMessage(this.Id, resId);
                return;
            }

			ServerNet.SendResLeaveRoomMapMessage(this.Id, 0);
		}
	}
	void OnRecvReqRoomBattleBeReadyMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqRoomBattleBeReadyMessage recieved !");
		if(message is Map.ReqRoomBattleBeReadyMessage recvedMsg)
		{
            var playerInfo = ServerNetManager.Instance.GetPlayer(this.Id);
            if (null == playerInfo)
            {
                UnityEngine.Debug.LogError("[Server]:Player Has Not Logined Yet");
                return;
            }

			if(!ServerNetManager.Instance.TryGetPlayerRoom(playerInfo.playerId, out var roomInfo))
            {
				UnityEngine.Debug.LogError("[Server]:Player Has No Room");
				return;
			}

			IRaceLogic raceLogic = ServerNetManager.Instance.StartNewRace();
			roomInfo.raceId = raceLogic.Id;

			ServerNet.SendResBattleStartMessage(this.Id, 256);
		}
	}
	void OnRecvReqLockStepCommandMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqLockStepCommandMessage recieved !");
		if(message is Map.ReqLockStepCommandMessage recvedMsg)
		{
		}
	}
	void OnRecvReqBattleEndMessage(Google.Protobuf.IMessage message)
	{
		UnityEngine.Debug.Log("[Server]:Msg ReqBattleEndMessage recieved !");
		if(message is Map.ReqBattleEndMessage recvedMsg)
		{
		}
	}
}
